<?php

class Permission_Api_Permission
{

	protected $_perms = array();

	/**
	 * current cached role
	 */
	protected $_roleId = NULL;

	/**
	 * @param int $roleId
	 */
	function loadPerm($roleId)
	{
		$cacheKey = 'core/permission/' . $roleId;

		$data = Nuo_Cache::instance() -> read($cacheKey);

		if (FALSE == $data)
		{

			$table = Nuo::getTable('permission');

			$table2 = Nuo::getTable('permission/action');

			$db = $table -> getAdapter();

			$select = $table -> select() -> from(array('act' => $table -> getName())) -> join(array('perm' => $table2 -> getName()), 'perm.action_id=act.action_id') -> where('act.role_id=?', (int)$roleId);

			$rows = $table -> fetchAll($select);

			$data = array();

			foreach ($rows as $row)
			{
				switch($row->data_type)
				{
					case 'json' :
					case 'array' :
						$data[$row -> action_key] = json_decode($row -> value, 1);
						break;
					default :
						$data[$row -> action_key] = $row -> value;
				}

			}

			Nuo_Cache::instance() -> write($cacheKey, $data);

		}
		return $data;
	}

	/**
	 * return roleId of guest
	 */
	function getGuestRoleId()
	{
		return 7;
	}

	/**
	 * get permission
	 */
	function getPerm($roleId, $action, $default = NULL)
	{
		if ($roleId == 1 or $roleId == 2)
		{
			return TRUE;
		}
		// if not current user, set role as custom user
		if (!$roleId)
		{
			$roleId = $this -> getGuestRoleId();
		}

		if ($roleId != $this -> _roleId)
		{
			$this -> _roleId = $roleId;
			$this -> _perms = $this -> loadPerm($roleId);
		}

		return isset($this -> _perms[$action]) ? $this -> _perms[$action] : ($this -> _perms[$action] = $default);
	}

	/**
	 * set role permission
	 */
	function setPermission($roleId, $data)
	{
		$table = Nuo::getTable('permission');

		$table2 = Nuo::getTable('permission/action');

		foreach ($data as $name => $value)
		{
			if (substr($name, 0, 1) == '_')
			{
				continue;
			}

			list($k1, $k2) = explode('_', $name, 2);

			if (!$k2)
			{
				continue;
			}

			$key = $k1 . '.' . $k2;

			$select = $table2 -> select() -> where('action_key=?', $key);

			$action = $table2 -> fetchRow($select);

			if (!$action)
			{
				continue;
			}

			$select = $table -> select() -> where('action_id=?', $action -> getId()) -> where('role_id=?', $roleId);

			$item = $table -> fetchRow($select);

			if (!$item)
			{
				$item = $table -> fetchNew();
				$item -> role_id = $roleId;
				$item -> action_id = $action -> getId();
			}

			$cleanedValue = NULL;
			switch($action->data_type)
			{
				case 'array' :
				case 'json' :
					$cleanedValue = json_encode($value);
					break;
				default :
					$cleanedValue = $value;
			}

			$item -> value = $cleanedValue;
			$item -> save();

		}
		return TRUE;
	}

	/**
	 * Get permission from database
	 * @access public
	 * @param   int  $id
	 * @return  array
	 */
	function loadPermission($id)
	{

		$table = Nuo::getTable('permission');

		$table2 = Nuo::getTable('permission/action');

		$select = $table -> select();
		$select -> from(array('act' => $table -> getName()));
		$select -> join(array('perm' => $table2 -> getName()), 'perm.action_id=act.action_id');
		$select -> where('act.role_id=?', (int)$id);

		$rows = $table -> fetchAll($select);

		$data = array();

		foreach ($rows as $row)
		{
			switch($row->data_type)
			{
				case 'json' :
				case 'array' :
					$data[$row -> action_key] = json_decode($row -> value, 1);
					break;
				default :
					$data[$row -> action_key] = $row -> value;
			}
		}

		return $data;
	}

}
